package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUser;
import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.history.HistoryItemVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.Utilities;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    LabelInfoService labelInfoService;

    @Autowired
    FacilityInfoService facilityInfoService;

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    BrowsingHistoryMapper browsingHistoryMapper;

    @Override
    public BigDecimal getMinRent(Long id) {
        return apartmentInfoMapper.getMinRent(id);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVoByid(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);



        //图片列表
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        lambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        List<GraphInfo> list = graphInfoService.list(lambdaQueryWrapper);
        List<GraphVo> graphVos = new ArrayList<>();
        for (GraphInfo graphInfo : list) {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphVos.add(graphVo);
        }
        apartmentDetailVo.setGraphVoList(graphVos);

        //标签列表
        List<LabelInfo> listLabe = labelInfoService.listByapid(id);
        apartmentDetailVo.setLabelInfoList(listLabe);

        //配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoService.lisByapId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        //租金最小值
       BigDecimal minRent = roomInfoMapper.minRentByApId(id);
        apartmentDetailVo.setMinRent(minRent);
       //公寓是否被删除
       Boolean isDelete =  apartmentInfoMapper.getIsDelete(id);
        apartmentDetailVo.setIsDelete(isDelete);








        return apartmentDetailVo;
    }
}




